﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace MicroTest
{
    [Serializable]
    class ClassInfo
    {
        public string ClassSymTitle { get; set; }
        /// <summary>
        /// 考场班级信息
        /// </summary>
        public string ClassRoomInfo { get; set; }
        /// <summary>
        /// 标签卡列表
        /// </summary>
        public List<StudentCard> CardList { get; set; }
        /// <summary>
        /// 试卷地址列表
        /// </summary>
        public List<string> ClassSymPageSerial { get; set; }
        /// <summary>
        /// 学生标签地址列表
        /// </summary>
        public List<string> ClassStudentCardPageSerial { get; set; }
        /// <summary>
        /// 试卷背景
        /// </summary>
        public Dictionary<string, Image> ClassSymImage { get; set; }

        public bool Login { get; set; }
        /// <summary>
        /// 考试数据存储路径
        /// </summary>
        //public string MicroTestDataFolder { get; private set; }
        //public string MicroTestGradeID { get; set; }
        //public string MicroTestClassID { get; set; }
        //public string MicroTestLianxiceID { get; set; }
        //public string MicroTestZhangjieID { get; set; }
        //public string MicroTestUserName { get; set; }

        /// <summary>
        /// 添加试卷背景图片
        /// </summary>
        /// <param name="pageSerial">图片地址</param>
        /// <param name="img">图片</param>
        public void AddSymPageImage(string pageSerial, Image img)
        {
            if (ClassSymImage == null)
                ClassSymImage = new Dictionary<string, Image>();
            if (ClassSymImage.ContainsKey(pageSerial) == false)
                ClassSymImage.Add(pageSerial, img);
            AddSymPageSerial(pageSerial);
        }
        /// <summary>
        /// 添加试卷图片地址到图片地址列表
        /// </summary>
        /// <param name="pageSerial">图片地址</param>
        private void AddSymPageSerial(string pageSerial)
        {
            if (ClassSymPageSerial == null)
                ClassSymPageSerial = new List<string>();
            foreach (var item in ClassSymPageSerial)
            {
                if (item.Equals(pageSerial))
                    return;
            }
            ClassSymPageSerial.Add(pageSerial);
        }
        /// <summary>
        /// 清除图片
        /// </summary>
        public void ClearSymPageImage()
        {
            if (ClassSymImage != null)
                ClassSymImage.Clear();
            if (ClassSymPageSerial != null)
                ClassSymPageSerial.Clear();
        }
        /// <summary>
        /// 通过学生姓名返回学生卡片信息
        /// </summary>
        /// <param name="studentName"></param>
        /// <returns></returns>
        public StudentCard GetStudentCardByName(string studentName)
        {
            foreach (var item in CardList)
            {
                if (studentName.Equals(item.Name))
                    return item;
            }
            return null;
        }
        /// <summary>
        /// 添加学生签到卡信息
        /// </summary>
        /// <param name="card">学生签到卡</param>
        public void AddStudentCard(StudentCard card)
        {
            if (CardList == null)
                CardList = new List<StudentCard>();
            CardList.Add(card);
            if (ClassStudentCardPageSerial == null)
                ClassStudentCardPageSerial = new List<string>();
            for (int i = 0; i < ClassStudentCardPageSerial.Count; i++)
            {
                if (ClassStudentCardPageSerial[i].Equals(card.PageId))
                    return;
            }
            ClassStudentCardPageSerial.Add(card.PageId);
        }
        /// <summary>
        /// 清除标签信息
        /// </summary>
        public void ClearStudentCard()
        {
            if (CardList != null)
                CardList.Clear();
            if (ClassStudentCardPageSerial != null)
                ClassStudentCardPageSerial.Clear();
        }
        /// <summary>
        /// 获取学生信息
        /// </summary>
        /// <param name="pageID"></param>
        /// <param name="nx"></param>
        /// <param name="ny"></param>
        /// <returns></returns>
        public string GetStudentName(string pageID, int nx, int ny)
        {
            string name = string.Empty;
            if (CardList == null)
            {
                ELogger.Error("学生列表信息为空");
                return name;
            }

            foreach (var item in CardList)
            {
                if (item.PageId.Equals(pageID) && nx >= item.LeftBound && nx <= item.LeftBound + item.CardWidth
                    && ny >= item.TopBound && ny <= item.TopBound + item.CardHeight)
                {
                    name = item.Name;
                    break;
                }
            }
            return name;
        }
        /// <summary>
        /// 判断当前地址是否为学生签到卡地址
        /// </summary>
        /// <param name="pageSerial"></param>
        /// <returns></returns>
        public bool IsStudentCardPage(string pageSerial)
        {
            if (ClassStudentCardPageSerial == null)
                return false;
            bool ret = false;
            for (int i = 0; i < ClassStudentCardPageSerial.Count; i++)
            {
                if (ClassStudentCardPageSerial[i].Equals(pageSerial))
                {
                    ret = true;
                    break;
                }
            }
            return ret;
        }
        /// <summary>
        /// 判断当前地址是否为试卷地址
        /// </summary>
        /// <param name="pageSerial"></param>
        /// <returns></returns>
        public bool IsPaperPageSerial(string pageSerial)
        {
            if (ClassSymPageSerial == null)
                return false;
            bool ret = false;
            for (int i = 0; i < ClassSymPageSerial.Count; i++)
            {
                if (ClassSymPageSerial[i].Equals(pageSerial))
                {
                    ret = true;
                    break;
                }
            }
            return ret;
        }
        public bool IniExam()
        {
            //Program.TEST_KEY = fs.TestKey;
            //string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\MicroTest";

            //if (!Directory.Exists(folder))
            //    Directory.CreateDirectory(folder);
            //Program.MTInfo.MicroTestDate = DateTime.Now;
            //string dateStr = DateTime.Now.ToString("yyyy-MM-dd");
            //folder = folder + "\\" + dateStr;
            //if (!Directory.Exists(folder))
            //    Directory.CreateDirectory(folder);
            //Program.DATA_ROOT = folder;
            //folder = folder + "\\" + MicroTestID;
            //if (!Directory.Exists(folder))
            //    Directory.CreateDirectory(folder);
            //Program.DATA_FOLDER = folder;
            //MicroTestDataFolder = folder;
            //ELogger.Trace(string.Format("微测密钥:{0}", Program.TEST_KEY));
            //ELogger.Trace(string.Format("微测数据存储路径:{0}", Program.DATA_FOLDER));
            return true;
        }
    }
}
